﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Core;
using IglooCoder.Commons.WcfNhibernate;
using NHibernate;
using NHibernate.Linq;

namespace Infrastucture
{
   public  class HppRepository : BaseNHibernateRepository<Hpp>
   {
       

       public HppRepository(ISessionStorage session)
       
           : base(session)
       {
          
       }


        public void SaveHppList(IList<Hpp> hpps)
        {
            StringBuilder sb = new StringBuilder();
            foreach (var hpp in hpps)
            {
                sb.Append("Delete from Hpp Where SerialNumber='" + hpp.SerialNumber +
                                       "' AND ReffNumber='" + hpp.ReffNumber + "';");
            }

            int res = Session.CreateSQLQuery(sb.ToString()).ExecuteUpdate();

                foreach (var hpp in hpps)
                {
                    Session.CreateSQLQuery("Delete from Hpp Where SerialNumber='" + hpp.SerialNumber +
                                           "' AND ReffNumber='" + hpp.ReffNumber + "'");
                    Session.SaveOrUpdate(hpp);
                }
            
        }


       public Hpp FindLastHpp(string serialNumber,DateTime tanggal)
       {
           var Hpp = Session.Linq<Hpp>().Where(x => x.SerialNumber == serialNumber && x.Tanggal <= tanggal)
                    .OrderByDescending(x => x.Tanggal).FirstOrDefault();
           if (Hpp == null)
               Hpp = new Hpp();

           return Hpp;
       }


       public void DeleteHpp(string ReferenceNumber)
       {

           ISQLQuery command =
               Session.CreateSQLQuery("Delete from Hpp  Where ReffNumber='" + ReferenceNumber + "'");
           int res = command.ExecuteUpdate();
       }
       

    
    }
}
